package com.dingwen.treasure.mybatisplus.aspect;

import cn.hutool.core.lang.Pair;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.dingwen.treasure.base.constant.SysConfigConstant;
import com.dingwen.treasure.base.enums.DicTypeEnum;
import com.dingwen.treasure.base.enums.StatusEnum;
import com.dingwen.treasure.base.pojo.vo.Result;
import com.dingwen.treasure.core.aspect.AbstractDicAspect;
import com.dingwen.treasure.model.entity.mybatisplus.SysDictionary;
import com.dingwen.treasure.mybatisplus.service.SysDictionaryService;
import com.dingwen.treasure.mybatisplus.util.SysConfigUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 　DicAspectMybatisPlusImpl
 * 　@author dingwen
 * 　@date 2022/7/10
 */
@Component
@Aspect
public class DicAspectMybatisPlusImpl extends AbstractDicAspect {

    /**
     * sysConfigUtil
     */
    @Resource
    private SysConfigUtil sysConfigUtil;

    /**
     * sysDictionaryService
     */
    @Resource
    private SysDictionaryService sysDictionaryService;

    /**
     * 字典切入点表达式
     */
    @Override
    @Pointcut(value = "execution(* com.dingwen.treasure..*Controller.get*(..))")
    public void dicPointCut() {

    }

    /**
     * 围绕增强进行字段翻译
     *
     * @param proceedingJoinPoint 继续连接点
     * @return {@link Object}
     */
    @Around("dicPointCut()")
    @Override
    public Object around(ProceedingJoinPoint proceedingJoinPoint) {
        return super.around(proceedingJoinPoint);
    }

    /**
     * 执行翻译
     *
     * @param result 后果
     * @return {@link Object}
     */
    @Override
    protected Object executeTranslate(Result result) {
        // 获取数据
        Object data = result.getData();

        // 分页数据
        if (data instanceof IPage) {
            List<?> records = ((IPage<?>) data).getRecords();
            List<JSONObject> jsonObjects = parseList(records);
            ((IPage<?>) data).setRecords((List) jsonObjects);
            result.setData(data);
            return result;
        }

        // 列表数据
        if (data instanceof List) {
            List<JSONObject> jsonObjects = parseList((List) data);
            result.setData(jsonObjects);
            return result;
        }

        // 单条数据
        JSONObject jsonObject = parse(data);
        result.setData(jsonObject);

        return result;
    }

    /**
     * 执行转换
     *
     * @param code code
     * @param name name
     * @return {@link Pair}
     */
    @Override
    protected Pair<String, String> executeParse(String code, String name) {
        // 获取系统配置字段翻译前缀
        String suffix = sysConfigUtil.getValue(SysConfigConstant.DIC_TRANSLATE_SUFFIX);

        // 逻辑删除 mybatis plus 已经自动完成
        LambdaQueryWrapper<SysDictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictionary::getDicType, DicTypeEnum.COMMON)
                .eq(SysDictionary::getDicCode, code)
                .eq(SysDictionary::getDicName, name)
                .eq(SysDictionary::getDicState, StatusEnum.NORMAL);

        SysDictionary sysDictionary = sysDictionaryService.getOne(queryWrapper);
        String translateValue = "";
        if (ObjectUtils.isNotEmpty(sysDictionary)) {
            translateValue = sysDictionary.getDicDescription();
        }
        return new Pair<>(name.concat(suffix), translateValue);
    }
}
